﻿#include "VAOneXMLAdaptorNodes.h"
#include "rapidxml.hpp"
#include <QFile>
#include <iostream>
#include <QDebug>
#include "FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITKAcousticsModel/FITKAbstractSEASubSysWithNodes.h"

namespace IO
{
    bool VAOneXMLAdaptorNodes::adaptR()
    {
        bool b = true;

        //数据检查
        if (nullptr == _node) return false;

        //数据转换成管理器结构
        Acoustics::FITKAcousticsDiscNodeList *dataManager = dynamic_cast<Acoustics::FITKAcousticsDiscNodeList*>(_dataObj);
        if (nullptr == dataManager) return false;

        //获取xml节点
        rapidxml::xml_node<char>* node = _node->first_node("Node");

        int id = 0;
        double x = 0.0, y = 0.0, z = 0.0;

        //循环获取点数据
        //<Node node_id="2" x="1.41421" y="1.41421" z="0"/>
        while (nullptr != node)
        {
            bool bOk = true;
            id = readIntAttribute(node, "node_id", &bOk);
            if (true != bOk)
            {
                qDebug()<<"Node id read error";
                return false;
            }
            x = readDoubleAttribute(node, "x", &bOk);
            if (true != bOk)
            {
                qDebug()<<"Node x value read error";
                return false;
            }

            y = readDoubleAttribute(node, "y", &bOk);
            if (true != bOk)
            {
                qDebug()<<"Node y value read error";
                return false;
            }

            z = readDoubleAttribute(node, "z", &bOk);
            if (true != bOk)
            {
                qDebug()<<"Node z value read error";
                return false;
            }

            //添加节点
            dataManager->addNode(id, x, y, z);

            //变量下一节点
            node = node->next_sibling("Node");
        }
        // 返回操作是否成功
        return b;
    }

    bool VAOneXMLAdaptorNodes::adaptW()
    {
        //数据有效检查
        Acoustics::FITKAcousticsDiscNodeList *dataManager = dynamic_cast<Acoustics::FITKAcousticsDiscNodeList*>(_dataObj);
        if ((nullptr == dataManager) || (nullptr == _node)) return false;

        //获取列表
        QList<int> listId;
        dataManager->getAllNodeID(listId);
        for (auto id : listId)
        {
            //获取数据节点
            Acoustics::DiscNode *data = (Acoustics::DiscNode *)dataManager->getNodeByID(id);
            if (nullptr == data)
            {
                qDebug()<<"Node get failed";
                return false;
            }

            //创建xml节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "Node");
            if (nullptr == node)
            {
                qDebug()<<"Node create failed";
                return false;
            }

            //单个节点属性设置
            //<Node node_id="2" x="1.41421" y="1.41421" z="0"/>
            writeAttribute(node, "node_id", id);
            writeAttribute(node, "x", data->x());
            writeAttribute(node, "y", data->y());
            writeAttribute(node, "z", data->z());
            // 默认值
            writeAttribute(node, "note", "");
        }

        // 返回操作是否成功
        return true;
    }
}
